﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using NUnit.Core;
using System.Data;
using System.Data.Common;

namespace SpGetter
{
    public class SpGetter
    {
        private SqlConnection con;
        private DataSet Execute(string query)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = query;
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

        public SpGetter(string ip, string catalog, string id, string pwd)
        {
            string constring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", ip, catalog, id, pwd);
            con = new SqlConnection(constring);
        }

        public bool connection_test()
        {
            try
            {
                con.Open();
                return true;
            }
            catch
            {
                con.Close();
                return false;
            }
        }

        public Procedure get_procedure(string spname)
        {
            string query = string.Format(@"select convert(int, sc.number), sc.[text] from syscomments sc inner join sysobjects so on sc.id = so.id where so.uid = user_id(N'dbo') and so.name = N'{0}' order by sc.number, sc.colid", spname);
            DataSet ds = Execute(query);
            Procedure sp = new Procedure();
            sp.Content = ds.Tables[0].Rows[0][1].ToString();
            sp.Name = spname;
            return sp;
        }

    }
}
